#include <stdio.h>
#include <chipmunk.h>
#include "super.h"

#define MAX_OBJS 100

#ifdef DREAMCAST
extern uint8 romdisk[];
KOS_INIT_FLAGS(INIT_DEFAULT | INIT_MALLOCSTATS);
KOS_INIT_ROMDISK(romdisk);
#endif

SE_ani *mario_ani[MAX_OBJS];
SE_input_joystick *joystick;
SE_input_state *state;

void cargarsprites()
{


		int i=0;
		for(i;i<MAX_OBJS;i++){		
			mario_ani[i]=SE_ani_init(1);
#ifdef DREAMCAST
			SE_ani_addframe(mario_ani[i],"/rd/run01.kmg",0,50);
#else
			SE_ani_addframe(mario_ani[i],"romdisk/run02.png",0,50);
#endif
		}

printf("1\n");

}


int main(void){

//SE_utils_log("iniciando demo");
SE_initgraphics();
SE_input_init();
printf("hay %d joysticks disponibles\n",SE_input_enum());
joystick = SE_input_open(0);

cargarsprites();

  // cpVect is a 2D vector and cpv() is a shortcut for initializing them.
  cpVect gravity = cpv(0, -100);
  
  // Create an empty space.
  cpSpace *space = cpSpaceNew();
  cpSpaceSetGravity(space, gravity);
  
  // Add a static line segment shape for the ground.
  // We'll make it slightly tilted so the ball will roll off.
  // We attach it to space->staticBody to tell Chipmunk it shouldn't be movable.
  cpShape *ground = cpSegmentShapeNew(space->staticBody, cpv(-20, 0), cpv(640, 0), 0);
  cpShapeSetFriction(ground, 1);
  cpShapeSetElasticity(ground, 0.9);

  cpShape *left = cpSegmentShapeNew(space->staticBody, cpv(-20, 0), cpv(-20, 640), 0);
  cpShapeSetFriction(left, 1);
  cpShapeSetElasticity(left, 0.9);

  cpShape *right = cpSegmentShapeNew(space->staticBody, cpv(640, 0), cpv(640, 300), 0);
  cpShapeSetFriction(right, 1);
  cpShapeSetElasticity(right, 0.9);

  cpSpaceAddShape(space, ground);
  cpSpaceAddShape(space, right);
  cpSpaceAddShape(space, left);

  
  // Now let's make a ball that falls onto the line and rolls off.
  // First we need to make a cpBody to hold the physical properties of the object.
  // These include the mass, position, velocity, angle, etc. of the object.
  // Then we attach collision shapes to the cpBody to give it a size and shape.
  
  cpFloat radius = 50;
  cpFloat mass = 1;
  
  // The moment of inertia is like mass for rotation
  // Use the cpMomentFor*() functions to help you approximate it.
  cpFloat moment = cpMomentForCircle(mass, 0, radius, cpvzero);
  
  // The cpSpaceAdd*() functions return the thing that you are adding.
  // It's convenient to create and add an object in one line.

  
  // Now we create the collision shape for the ball.
  // You can create multiple collision shapes that point to the same body.
  // They will all be attached to the body and move around to follow it.
cpShape *ballShape[MAX_OBJS];
cpBody *ballBody[MAX_OBJS];

	int i=0;
	for(i;i<MAX_OBJS;i++){
		ballBody[i] = cpSpaceAddBody(space, cpBodyNew(mass, moment));
		cpBodySetPos(ballBody[i], cpv(200+i, i*200+1000));
		ballShape[i] = cpSpaceAddShape(space, cpCircleShapeNew(ballBody[i], radius, cpvzero));
		cpShapeSetFriction(ballShape[i], 0.7);
		cpShapeSetElasticity(ballShape[i], 0.9);
	}

printf("2\n");
  // Now that it's all set up, we simulate all the objects in the space by
  // stepping forward through time in small increments called steps.
  // It is *highly* recommended to use a fixed size time step.
 /* cpFloat timeStep = 1.0/60.0;
  for(cpFloat time = 0; time < 2; time += timeStep){
    cpVect pos = cpBodyGetPos(ballBody);
    cpVect vel = cpBodyGetVel(ballBody);
    printf(
      "Time is %5.2f. ballBody is at (%5.2f, %5.2f). It's velocity is (%5.2f, %5.2f)\n",
      time, pos.x, pos.y, vel.x, vel.y
    );
    
    cpSpaceStep(space, timeStep);
  }*/
cpFloat timeStep = 1.0/60.0;
cpFloat time = 0;
int quit=0;

printf("3\n");
cpVect pos;
	while(quit == 0)
	{
			/*borra la pantalla con un color*/
			SE_clearscreen(1.0f,1.0f,1.0f,0.0f);

			int x=0;			
			for(x=0;x<MAX_OBJS;x++){
				printf("5\n");
				pos = cpBodyGetPos(ballBody[x]);
				printf("6\n");
				SE_ani_position(mario_ani[x],pos.x,500.0f-pos.y,0);		
				printf("7\n");
				SE_ani_draw(mario_ani[x]);
			}
			/*flipea la escena o cuadro*/	
			SE_flip();
			SE_timer_waitframerate();
			cpSpaceStep(space, timeStep);
			time += timeStep;

			state=SE_input_poll(&joystick);
			if(state){	
					if(state->buttons & SE_QUIT) /* Handle Joystick Motion */
							quit=1;
			}
	}

  
  // Clean up our objects and exit!
  /*cpShapeFree(ballShape);
  cpBodyFree(ballBody);
  cpShapeFree(ground);
  cpSpaceFree(space);*/
  
  return 0;
}
